package yxy.game.pm2.module.active.openservicerush;

import yxy.apple.logger.Logger;
import yxy.game.pm2.bean.module.RedissonLock;
import yxy.game.pm2.bean.pubsub.TaskPublisher;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.module.daily.DailyModule;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.player.UnionPlayer;

import java.util.ArrayList;
import java.util.Map;

/**
 * 记录第一名完成任务的数据
 *
 * @ClassName OpenServiceRushRecord
 * @Author jingtao-zheng
 * @Date 2022/7/11 15:10
 * @Version 1.0
 **/
public class OpenServiceRushRecord {
    private final static int HERO_COMPETITION = 1001;//武将比拼
    private final static int ARENA_HEGEMONY = 1002;//竞技争雄
    private final static int LEVEL_UP = 1003;//等级提升
    private final static int WORK_TOGETHER = 1004;//齐心协力

    private final static String LOCKKEY = "OpenServiceRush";

    protected static Logger logger = Logger.create(OpenServiceRushRecord.class);

    /**
     * 武将任务记录
     */
    static public void recordFirstCompleteHeroTask(String playerCode) {
        //任务记录
        handle(playerCode, TaskType.指标型_拥有过武将种类_达到, 1, HERO_COMPETITION);
    }

    /**
     * 竞技场任务记录
     */
    static public void recordFirstCompleteAreanTask(String playerCode, int socre) {
        //任务记录
        handle(playerCode, TaskType.指标型_竞技场分数_达到, socre, ARENA_HEGEMONY);
    }

    /**
     * 武将等级记录
     */
    static public void recordFirstCompleteHeroLevelTask(String playerCode, int socre, int aptitude) {
        //任务记录
        heroLevelhandle(playerCode, TaskType.指标型_将武将等级_达到, socre, aptitude, LEVEL_UP);
    }

    /**
     * 联盟boos
     */
    static public void recordFirstCompleteBoosTask(String playerCode, int score) {
        UnionPlayer unionPlayer = UnionPlayer.get(playerCode);
        UnionBean union = UnionBean.get(unionPlayer.getUnionCode());
        ArrayList<String> menlist = new ArrayList<>();

        String leader = union.getLeader();
        ArrayList<String> memberList = union.getMemberList();
        ArrayList<String> applyList = union.getApplyList();
        menlist.add(leader);
        menlist.addAll(memberList);
        menlist.addAll(applyList);
        //给工会每个人更新任务记录
        for (String playercode : menlist) {
            handle(playercode, TaskType.指标型_击杀联盟boos次数_达到, score, WORK_TOGETHER);
        }
    }

    /**
     * 个人记录联盟boos
     */
    static public void recordPersonalBoosTask(String playerCode, int score) {
        handle(playerCode, TaskType.指标型_击杀联盟boos次数_达到, score, WORK_TOGETHER);
    }

    /**
     * 记录第一名完成任务的玩家
     * Record First to complete the task
     */
    private static void handle(String playerCode, TaskType type, long value, int actType) {
        handle(playerCode, type, value, -1, actType);
    }

    /**
     * 记录第一名完成任务的玩家
     * Record First to complete the task
     * 武将等级特殊处理
     */
    private static void heroLevelhandle(String playerCode, TaskType type, long value, int value2, int actType) {
        handle(playerCode, type, value, value2, actType);
    }

    /**
     * 重写了任务记录,加入记录第一名完成任务的记录
     *
     * @param acttype 活动类型
     */
//    static private void handle(String playerCode, TaskType type, long value, int condition, int acttype) {
//        // XXX 临时增加的处理 避免跨日后 日常奖励没有还原
//        DailyModule.get(playerCode);
//
//        boolean hasfinished = false;
////        RLock lock = null;
////        try {
////            lock = RedisManager.client(RedisManager.getInstance().getConnection(RedisType.REDIS_GAME)).getLock(LOCKKEY);
////        } catch (RedisException e) {
////            e.printStackTrace();
////        }
//        try {
//            if (Task.lockTask(playerCode)) {
//                logger.info("线程：%s拿到锁#################时间:%s", Thread.currentThread().getName(), DateTime.now().getMillis());
//                Map<String, Task> tasks = Task.getByType(playerCode, type);
//                for (Task task : tasks.values()) {
//                    switch (type.getCountType()) {
//                        case 累计:
//                            task.countRecord(value, condition, playerCode, acttype, task.getDataId(), task.getCode());
//                            break;
//                        case 达到:
//                            task.checkRecord(value, condition, playerCode, acttype);
//                            break;
//                    }
//                    if (task.isFinish()) {
//                        hasfinished = true;
//                    }
//                }
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            Task.unlockTask(playerCode);
//            logger.info("线程：%s解锁#################时间:%s", Thread.currentThread().getName(), DateTime.now().getMillis());
//        }
//        if (hasfinished) {
//            TaskPublisher.getInstance().publish(playerCode);
//        }
//    }


    /**
     * 重写了任务记录,加入记录第一名完成任务的记录
     *
     * @param acttype 活动类型
     */
    static private void handle(String playerCode, TaskType type, long value, int condition, int acttype) {
        // XXX 临时增加的处理 避免跨日后 日常奖励没有还原
        DailyModule.get(playerCode);
        boolean hasfinished = RedissonLock.tryLock(() -> {
            boolean isfinis = false;
            Map<String, Task> tasks = Task.getByType(playerCode, type);
            for (Task task : tasks.values()) {
                switch (type.getCountType()) {
                    case 累计:
                        task.countRecord(value, condition, playerCode, acttype);
                        break;
                    case 达到:
                        task.checkRecord(value, condition, playerCode, acttype);
                        break;
                }
                if (task.isFinish()) {
                    isfinis = true;
                }
            }
            return isfinis;
        }, playerCode+LOCKKEY);
        //如果有任务完成就发送红点信息
        if (hasfinished) {
            TaskPublisher.getInstance().publish(playerCode);
        }
    }
}
